#include "summatorwidget.h"
#include <QMessageBox>

SummatorWidget::SummatorWidget(QWidget *parent) :
    QWidget(parent)
{
    QVBoxLayout *vlay = new QVBoxLayout();

    result = new QLabel("SUM = ???", this);
    vlay->addWidget(result);

    QGridLayout *grid = new QGridLayout();

    grid->addWidget(new QLabel("A:", this), 0, 0);
    aField = new QLineEdit(this);
    grid->addWidget(aField, 0, 1);
    grid->addWidget(new QLabel("B:", this), 1, 0);
    bField = new QLineEdit(this);
    grid->addWidget(bField, 1, 1);

    vlay->addItem(grid);
    submit = new QPushButton("Set sum", this);
    vlay->addWidget(submit);

    this->setLayout(vlay);

    connect(submit, SIGNAL(clicked()), SLOT(setSum()));
}

void SummatorWidget::setSum()
{
    QString aStr, bStr;
    aStr = aField->text();
    bStr = bField->text();

    bool ok;
    int a = aStr.toInt(&ok);
    if (!ok)
    {
        QMessageBox msg(this);
        msg.setText("Enter A, please!");
        msg.exec();
        return;
    }

    int b = bStr.toInt(&ok);
    if (!ok)
    {
        QMessageBox msg(this);
        msg.setText("Enter B, please!");
        msg.exec();
        return;
    }

    sum.setA(a);
    sum.setB(b);
    int res = sum.sum();

    result->setText(QString("SUM = %1").arg(res));
}
